import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store/index";
import nprogress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import { rewardManagement } from "./modules/rewardManagement";
import { personnelRegulate } from "./modules/personnelRegulate";
import { chcking } from "./modules/chcking";
import { approve } from "./modules/approve";
import { groupAccount } from "./modules/groupAccount";
import { systemManage } from "./modules/systemManage";
import { enterpriseManage } from "./modules/enterpriseManage";
import { header } from "./modules/header";

nprogress.configure({
  showSpinner: false,
});
// 解决vue-router重复打开报错的问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);

const commonRoutes = [
  {
    path: "/homePages",
    name: "homePages",
    component: () => import("@/views/myAccountViewAccount/homepages/homePages.vue"),
    meta: {
      title: "主页",
    },
  },
  //header||辅助项
  ...header,
  //企业管理
  ...enterpriseManage,
  // 人力资源
  ...personnelRegulate,
  // 薪酬管理
  ...rewardManagement,
  //审批云
  ...approve,
  //考勤管理
  ...chcking,
  //集团账套
  ...groupAccount,
  //系统管理
  ...systemManage,
];

const routes = [
  {
    path: "/",
    name: "layout",
    component: () => import("@/page/index"),
    children: commonRoutes,
  },
  {
    path: "/financialLogin",
    name: "financialLogin",
    component: () => import("@/page/index.vue"),
    children: commonRoutes,
  },
];

const router = new VueRouter({
  mode: "history",
  // base: process.env.BASE_URL,
  routes,
});
router.beforeEach((to, form, next) => {
  nprogress.start();
  // const callBack = (data) => data.reduce((p, c) => p.concat(c, callBack(c.children)), [])
  const RouteWhiteList = ["/", "/financialLogin"];
  const flag = store.state.common.uif;
  if (flag) {
    next();
  } else {
    if (RouteWhiteList.includes(to.path)) {
      next();
    } else {
      next("/");
    }
  }
});
router.beforeResolve((to, from, next) => {
  if (to.path === "/login") {
    store.commit("DEL_ALL_TAG");
  } else {
    if (to.meta.noAddTag) {
      nprogress.done();
    } else {
      if (to.path !== "/") {
        store.commit("ADD_TAG", {
          label: to.meta.title,
          value: to.path,
          query: to.query,
          close: true,
        });
      }
    }
  }
  next();
});
router.afterEach(() => {
  nprogress.done();
});

export default router;
